﻿var topPanel = null;
var portalPanel = null;
var viewport = {};
var global_page = {
	viewport : viewport,
	mode :
	// "edit"
	"view"
};

var init_edit = function() {
	var _viewport = new Ext.Viewport({});
	var oringalWidth = _viewport.getSize().width;
	var marginsArray = model.margins.split(" ");
	var margin_right = null;
	var margin_left = null;
	if (null != viewWidth && viewWidth != 0) {
		var curWidth = oringalWidth - viewWidth;
		margin_right = curWidth / 2 > 0 ? curWidth / 2 : curWidth;
		margin_left = curWidth / 2 > 0 ? curWidth / 2 : 0;
	} else if (null == viewWidth || viewWidth == 0) {
		margin_right = marginsArray[1];
		margin_left = marginsArray[3];
	}
	topPanel = new MyTopPanel();
	var westTree = MyChannalTree_;
	new Ext.tree.TreeSorter(westTree, {
				folderSort : true,
				dir : 'asc',
				sortType : function(node1) {
					return parseInt(node1.attributes.sortorder);
				}
			});
	var centerPanel_edit = new MyCenterPanel();
	var margin_top = marginsArray[0];
	var margin_bottom = marginsArray[2];
	var topPanel_margins = {
		top : margin_top,
		right : margin_right,
		bottom : margin_bottom,
		left : margin_left
	};
	var topPanel_margins_ = margin_top + ' ' + margin_right + ' ' + '0' + ' '
			+ margin_left;
	var westTree_margins_ = 0 + ' ' + '-' + topPanel_margins.left + ' '
			+ topPanel_margins.bottom + ' ' + topPanel_margins.left;
	var centerPanel_edit_margins_ = "'" + 0 + " " + topPanel_margins.right
			+ " " + topPanel_margins.bottom + " " + topPanel_margins.left + "'";

	var west_tmp = {
		id : "cmp_west_container_edit",
		title : '频道列表',
		region : 'west',
		split : true,
		width : 230,
		minSize : 175,
		maxSize : 600,
		margins : westTree_margins_,
		rootVisible : false,
		lines : false,
		autoScroll : true,
		animCollapse : true,
		animate : true,
		collapseMode : 'mini',
		items : [westTree]
	};
	var top_tmp = {
		margins : topPanel_margins_,
		id : "cmp_north_container_edit",
		region : 'north',
		layout : "fit",
		split : true,
		animate : true,
		collapseMode : 'mini',
		minSize : 26,
		maxSize : 26,
		items : [topPanel],
		height : 26,
		listeners : {
			'expand' : function() {
				swith2edit();
			},
			'collapse' : function() {
				swith2view();
			}
		}
	};
	var center_tmp = {
		id : "cmp_center_container_edit",
		region : 'center',
		layout : "fit",
		margins : (function() {
			return centerPanel_edit_margins_
		})(),
		items : [centerPanel_edit]
	};
	viewport = new Ext.Viewport({
				id : 'viewport_edit',
				layout : 'border',
				monitorResize : true,
				listeners : {
					'resize' : function(e, adjWidth, adjHeight, rawWidth,
							rawHeight) {
						if (null != rawWidth && rawWidth != 'undified'
								&& (null != viewWidth && viewWidth != 0)) {
							document.execCommand('Refresh');
						}
					}
				},
				items : [top_tmp, west_tmp, center_tmp],
				topPanel : topPanel,
				westPanel : westTree,
				west_tmp : west_tmp,
				center_tmp : center_tmp,
				top_tmp : top_tmp,
				centerPanel_edit : centerPanel_edit
			});
	if (global_page.mode == 'view') {
		Ext.each(getCurPortlets(), function(tools) {
					Ext.getCmp(tools).getTool('pin').show();
					Ext.getCmp(tools).getTool('maximize').show();
					Ext.getCmp(tools).getTool('close').hide();
				});
		Ext.getCmp('save').hide();
		Ext.getCmp('reset').hide();
		setupDropTargets();
		// Ext.dd.DragDropMgr.lock();
		Ext.getCmp("cmp_west_container_edit").hide();
		// collapse
		Ext.getCmp('cmp_west_container_edit').collapse(true);
		Ext.getCmp('cmp_north_container_edit').collapse(true);
	}
	if (global_page.mode == 'edit') {
		// alert('edit');
		Ext.each(getCurPortlets(), function(tools) {
					// Ext.getCmp(tools).getTool('pin').hide();
					Ext.getCmp(tools).getTool('maximize').hide();
					Ext.getCmp(tools).getTool('close').show();
				});
		Ext.getCmp('text_mode_id').setText('浏览模式');
		Ext.getCmp('save').show();
		Ext.getCmp('reset').show();
		setupDropTargets();
		Ext.dd.DragDropMgr.unlock();
		// expand
		Ext.getCmp("cmp_west_container_edit").expand(true);
		Ext.getCmp("cmp_north_container_edit").show();
		Ext.getCmp('cmp_north_container_edit').expand(true);
	};
	// resize,refresh
	var aa = new loadIframeContent();
};
function loadIframeContent() {
	for (var i = 1; i <= portalPanel.layoutInfo.columns; i++) {
		var c = Ext.getCmp('column111' + i);
		c.items.each(function(portlet) {
			if (portlet.type == '1' || portlet.type == 1) {

				// var store = new Ext.data.Store({
				// proxy : new Ext.data.ScriptTagProxy({
				// url : portlet.contentUrl
				// }),
				// reader : new Ext.data.JsonReader({
				// root : 'messages',
				// image : 'image',
				// id : 'title'
				// }, [{
				// name : 'title',
				// mpping : 'title'
				// }, {
				// name : 'link',
				// mapping : 'link'
				// }]),
				// remoteSort : true
				// })
				// store.load();
				// debugObj(store.data);

				Ext.Ajax.request({
					url : portlet.contentUrl,
					method : 'get',
					scope : this,
					headers : 'Content-Type: text/json',
					success : function(response, op) {
						var json = response.responseText;
						var p = json.indexOf('{');
						json = json.substring(p);
						p = json.indexOf(';');
						var jstUrlContent = json.substring(0, p - 1);
						var jstObject = eval("(" + jstUrlContent + ")");
						jstShowObject = portlet.template.process(jstObject);
						portlet.items.items[0].body.dom.innerHTML = jstShowObject
						portlet.doLayout();
					},
					failure : function(response, op) {
						Ext.Msg.alert('访问' + portlet.contentUrl + '失败!');
					}
				});
			} else {
				portlet.setSrc(portlet.contentUrl);
				portlet.doLayout();
			}
		});
	}
}